package com.margo.project.page.service;


import java.util.List;
import java.util.UUID;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.margo.project.page.model.MargoUserVO;

@Transactional
@Service
public class MargoUserService {

	private Logger logger=LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	public int insert() {
		long begin=System.currentTimeMillis();
		Object[] insertArgs = new Object[0];
		for(int i=0;i<10000;i++) {
			 jdbcTemplate.update(
						"INSERT INTO margo_user(name, age, birth_date, sex, phone, account, state, remark, email, creator_id, creator, create_date, modifier_id, modifier, modify_date) VALUES ('阳阳', '28', '1995-02-08', '1', '15802705093', 'yangyang', '1', '测试账号', 'fxb208@126.com', '1', 'SuperAdmin', '2020-07-28 14:54:56', '1', 'SuperAdmin', '2020-07-28 14:55:09')",
						insertArgs);
		}
//		String[] insertArgs = new String[10000];
//		for(int i=0;i<insertArgs.length;i++) {
//			insertArgs[i]="INSERT INTO margo_user(name, age, birth_date, sex, phone, account, state, remark, email, creator_id, creator, create_date, modifier_id, modifier, modify_date) VALUES ('阳阳', '28', '1995-02-08', '1', '15802705093', 'yangyang', '1', '测试账号', 'fxb208@126.com', '1', 'SuperAdmin', '2020-07-28 14:54:56', '1', 'SuperAdmin', '2020-07-28 14:55:09')";
//		}
//		jdbcTemplate.batchUpdate(insertArgs);
		long cost=System.currentTimeMillis()-begin;
		logger.info("插入时间:"+cost);
		return 1;	
	}
	
	public int insertuuid() {
		String[] arrUUid=arrUUid();
		long begin=System.currentTimeMillis();
		//logger.info("开始插入  :"+begin);
		Object[] insertArgs = new Object[1];
		for(int i=0;i<10000;i++) {
			insertArgs[0]=arrUUid[i];
			 jdbcTemplate.update(
						"INSERT INTO margo_user_uuid(id,name, age, birth_date, sex, phone, account, state, remark, email, creator_id, creator, create_date, modifier_id, modifier, modify_date) VALUES (?,'阳阳', '28', '1995-02-08', '1', '15802705093', 'yangyang', '1', '测试账号', 'fxb208@126.com', '1', 'SuperAdmin', '2020-07-28 14:54:56', '1', 'SuperAdmin', '2020-07-28 14:55:09')",
						insertArgs);
		}
//		String[] insertArgs = new String[10000];
//		for(int i=0;i<insertArgs.length;i++) {
//			insertArgs[i]="INSERT INTO margo_user(name, age, birth_date, sex, phone, account, state, remark, email, creator_id, creator, create_date, modifier_id, modifier, modify_date) VALUES ('阳阳', '28', '1995-02-08', '1', '15802705093', 'yangyang', '1', '测试账号', 'fxb208@126.com', '1', 'SuperAdmin', '2020-07-28 14:54:56', '1', 'SuperAdmin', '2020-07-28 14:55:09')";
//		}
//		jdbcTemplate.batchUpdate(insertArgs);
		long cost=System.currentTimeMillis()-begin;
		logger.info("插入时间:"+cost);
		return 1;	
	}
	
	
	/**
	 * 	查询全部数据
	 * @return
	 */
	public int select() {
		long begin=System.currentTimeMillis();
		RowMapper<MargoUserVO> rowMapper = new BeanPropertyRowMapper<MargoUserVO>(MargoUserVO.class);
		List<MargoUserVO> lst =jdbcTemplate.query("select * from margo_user", rowMapper);
	
		
		long cost=System.currentTimeMillis()-begin;
		logger.info(Thread.currentThread().getName()+"查询话费时间"+":"+cost);
		logger.info("长度:"+lst.size());
		return lst.size();
	}
	
	/**
	 * 	分页查询部分数据
	 * @return
	 */
	public int selectPage() {
		long begin=System.currentTimeMillis();
		RowMapper<MargoUserVO> rowMapper = new BeanPropertyRowMapper<MargoUserVO>(MargoUserVO.class);
		List<MargoUserVO> lst =jdbcTemplate.query("select * from margo_user limit 0,100", rowMapper);
		long cost=System.currentTimeMillis()-begin;
		logger.info(Thread.currentThread().getName()+"查询话费时间"+":"+cost);
		logger.info("长度:"+lst.size());
		return lst.size();
	}
	
	public int selectList() {
		long begin=System.currentTimeMillis();
		RowMapper<MargoUserVO> rowMapper = new BeanPropertyRowMapper<MargoUserVO>(MargoUserVO.class);
		List<MargoUserVO> lst =jdbcTemplate.query("select * from margo_user", rowMapper);
		List<MargoUserVO> lst1 =jdbcTemplate.query("select * from margo_user", rowMapper);
		List<MargoUserVO> lst2 =jdbcTemplate.query("select * from margo_user", rowMapper);
		List<MargoUserVO> lst3 =jdbcTemplate.query("select * from margo_user", rowMapper);
		List<MargoUserVO> lst4 =jdbcTemplate.query("select * from margo_user", rowMapper);
		List<MargoUserVO> lst5 =jdbcTemplate.query("select * from margo_user", rowMapper);
		List<MargoUserVO> lst6 =jdbcTemplate.query("select * from margo_user", rowMapper);
		List<MargoUserVO> lst7 =jdbcTemplate.query("select * from margo_user", rowMapper);
		List<MargoUserVO> lst8 =jdbcTemplate.query("select * from margo_user", rowMapper);
		List<MargoUserVO> lst9 =jdbcTemplate.query("select * from margo_user", rowMapper);
		
		List<MargoUserVO> lst10 =jdbcTemplate.query("select * from margo_user", rowMapper);
		List<MargoUserVO> lst11 =jdbcTemplate.query("select * from margo_user", rowMapper);
		List<MargoUserVO> lst12 =jdbcTemplate.query("select * from margo_user", rowMapper);
		List<MargoUserVO> lst13 =jdbcTemplate.query("select * from margo_user", rowMapper);
		List<MargoUserVO> lst14 =jdbcTemplate.query("select * from margo_user", rowMapper);
		
		List<MargoUserVO> lst15 =jdbcTemplate.query("select * from margo_user", rowMapper);
		List<MargoUserVO> lst16 =jdbcTemplate.query("select * from margo_user", rowMapper);
		List<MargoUserVO> lst17 =jdbcTemplate.query("select * from margo_user", rowMapper);
		List<MargoUserVO> lst18 =jdbcTemplate.query("select * from margo_user", rowMapper);
		List<MargoUserVO> lst19 =jdbcTemplate.query("select * from margo_user", rowMapper);
		
		long cost=System.currentTimeMillis()-begin;
		logger.info(Thread.currentThread().getName()+"查询话费时间"+":"+cost);
		logger.info("长度:"+lst.size());
		logger.info("长度:"+lst1.size());
		logger.info("长度:"+lst2.size());
		logger.info("长度:"+lst3.size());
		logger.info("长度:"+lst4.size());
		logger.info("长度:"+lst5.size());
		logger.info("长度:"+lst6.size());
		logger.info("长度:"+lst7.size());
		logger.info("长度:"+lst8.size());
		logger.info("长度:"+lst9.size());
		
		logger.info("长度:"+lst10.size());
		logger.info("长度:"+lst11.size());
		logger.info("长度:"+lst12.size());
		logger.info("长度:"+lst13.size());
		logger.info("长度:"+lst14.size());
		logger.info("长度:"+lst15.size());
		logger.info("长度:"+lst16.size());
		logger.info("长度:"+lst17.size());
		logger.info("长度:"+lst18.size());
		logger.info("长度:"+lst19.size());
		cost=System.currentTimeMillis()-begin;
		logger.info(Thread.currentThread().getName()+"查询话费时间"+":"+cost);
		return lst.size();
	}
	
	public void multSelect() {
		logger.info("开始请求");
		CyclicBarrier cyclicBarrier=new CyclicBarrier(5);
		for(int i=0;i<5;i++) {
			Thread thread=new Thread(new Runnable() {
				@Override
				public void run() {
					try {
						logger.info(Thread.currentThread().getName()+"执行任务");
						cyclicBarrier.await();
					} catch (InterruptedException | BrokenBarrierException e) {
						logger.error(e.getMessage(),e);
					}
					select();
				}
			});
			thread.start();
		}
		logger.info("查询结束");
	}
	
	private String[] arrUUid() {
		String[] arrUUid=new String[10000];
		for(int i=0;i<10000;i++) {
			arrUUid[i]=UUID.randomUUID().toString().replace("-", "");
		}
		return arrUUid;
	}
}
